package {{packageName}};

import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.testcontainers.kafka.KafkaContainer;
import org.testcontainers.utility.DockerImageName;

public class KafkaTestContainerExtension implements BeforeAllCallback {

  private static final AtomicBoolean kafkaContainerStarted = new AtomicBoolean(false);
  private static KafkaContainer kafkaContainer;

  @Override
  public void beforeAll(final ExtensionContext context) {
    if (!kafkaContainerStarted.get()) {
      kafkaContainer = new KafkaContainer(DockerImageName.parse("{{kafkaDockerImage}}")).withNetwork(null);
      // Waiting this issue https://github.com/testcontainers/testcontainers-java/issues/9506 to remove this line
      kafkaContainer.withEnv("KAFKA_LISTENERS", "PLAINTEXT://:9092,BROKER://:9093,CONTROLLER://:9094");
      kafkaContainer.start();
      kafkaContainerStarted.set(true);
      System.setProperty("kafka.bootstrap.servers", kafkaContainer.getBootstrapServers());
    }
  }
}
